{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import mxnet as mx\n",
    "from mxnet import init, gluon, nd, autograd, image\n",
    "from mxnet.gluon import nn\n",
    "from mxnet.gluon.data import vision\n",
    "from mxnet.gluon.model_zoo import vision as models\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "from tqdm import tqdm\n",
    "import cv2\n",
    "import h5py\n",
    "import os\n",
    "from glob import glob\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "%matplotlib inline\n",
    "%config InlineBackend.figure_format = 'retina'\n",
    "\n",
    "ctx = mx.gpu()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def accuracy(output, labels):\n",
    "    return nd.mean(nd.argmax(output, axis=1) == labels).asscalar()\n",
    "\n",
    "def evaluate(net, data_iter):\n",
    "    loss, acc, n = 0., 0., 0.\n",
    "    steps = len(data_iter)\n",
    "    for data, label in data_iter:\n",
    "        data, label = data.as_in_context(ctx), label.as_in_context(ctx)\n",
    "        output = net(data)\n",
    "        acc += accuracy(output, label)\n",
    "        loss += nd.mean(softmax_cross_entropy(output, label)).asscalar()\n",
    "    return loss/steps, acc/steps"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,(20580, 2048)\n"
     ]
    }
   ],
   "source": [
    "n = len(glob('Images/*/*.jpg'))\n",
    "\n",
    "mean = np.array([0.485, 0.456, 0.406])\n",
    "std = np.array([0.229, 0.224, 0.225])\n",
    "\n",
    "net = models.get_model('inceptionv3', pretrained=True, ctx=ctx)\n",
    "features = []\n",
    "for j in range(0,161):\n",
    "    print(j, end='-')\n",
    "    i = 0\n",
    "    temp = nd.zeros((128, 3, 299, 299))\n",
    "    for file_name in glob('Images/*/*.jpg')[128*j:128*(j+1)]:\n",
    "        img = cv2.imread(file_name)\n",
    "        img_299 = ((cv2.resize(img, (299, 299))[:,:,::-1] / 255.0 - mean) / std).transpose((2, 0, 1))\n",
    "        temp[i] = nd.array(img_299)\n",
    "        nd.waitall()\n",
    "        i = i + 1\n",
    "    if j == 160:\n",
    "        temp = temp[0:100]\n",
    "    data_iter_299 = gluon.data.DataLoader(gluon.data.ArrayDataset(temp), batch_size=128)\n",
    "    for data in data_iter_299:\n",
    "        feature = net.features(data.as_in_context(mx.gpu()))\n",
    "        feature = gluon.nn.Flatten()(feature)\n",
    "        features.append(feature.as_in_context(mx.cpu()))\n",
    "    nd.waitall()\n",
    "features = nd.concat(*features, dim=0)\n",
    "print(features.shape)\n",
    "nd.save('features_incep.nd', features)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "n = len(glob('Images/*/*.jpg'))\n",
    "\n",
    "mean = np.array([0.485, 0.456, 0.406])\n",
    "std = np.array([0.229, 0.224, 0.225])\n",
    "\n",
    "net = models.get_model('resnet152_v1', pretrained=True, ctx=ctx)\n",
    "features = []\n",
    "for j in range(0,161):\n",
    "    print(j,end='-')\n",
    "    i = 0\n",
    "    temp = nd.zeros((128, 3, 224, 224))\n",
    "    for file_name in glob('Images/*/*.jpg')[128*j:128*(j+1)]:\n",
    "        img = cv2.imread(file_name)\n",
    "        img_224 = ((cv2.resize(img, (224, 224))[:,:,::-1] / 255.0 - mean) / std).transpose((2, 0, 1))\n",
    "        temp[i] = nd.array(img_224)\n",
    "        nd.waitall()\n",
    "        i = i + 1\n",
    "    if j == 160:\n",
    "        temp = temp[0:100]\n",
    "    data_iter_224 = gluon.data.DataLoader(gluon.data.ArrayDataset(temp), batch_size=128)\n",
    "    for data in data_iter_224:\n",
    "        feature = net.features(data.as_in_context(mx.gpu()))\n",
    "        feature = gluon.nn.Flatten()(feature)\n",
    "        features.append(feature.as_in_context(mx.cpu()))\n",
    "    nd.waitall()\n",
    "features = nd.concat(*features, dim=0)\n",
    "print(features.shape)\n",
    "nd.save('features_res.nd', features)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
